package com.rabbit.eap.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rabbit.eap.common.lang.StringUtils;
import com.rabbit.eap.framework.common.api.ApiResult;
import com.rabbit.eap.framework.common.enums.MsgEnum;
import com.rabbit.eap.framework.common.service.impl.BaseServiceImpl;
import com.rabbit.eap.framework.core.pagination.PageInfo;
import com.rabbit.eap.framework.core.pagination.Paging;
import com.rabbit.eap.system.dto.SysNoticeDTO;
import com.rabbit.eap.system.entity.SysNotice;
import com.rabbit.eap.system.mapper.SysNoticeMapper;
import com.rabbit.eap.system.param.SysNoticePageParam;
import com.rabbit.eap.system.service.ISysNoticeService;
import com.rabbit.eap.system.vo.SysNoticeVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;

/**
 * @projectName: eap
 * @packageName: com.rabbit.eap.system.service.impl
 * @className SysNoticeServiceImpl
 * @author： rabbit
 * @version： 1.0
 * @since： 2020/06/06 23:01:20
 * <p>Description:
 * 通知公告表 服务实现类
 * </p>

 */
@Slf4j
@Service
public class SysNoticeServiceImpl extends BaseServiceImpl<SysNoticeMapper, SysNotice> implements ISysNoticeService {

    @Autowired
    private SysNoticeMapper sysNoticeMapper;

    /**
     * @Description: 添加通知公告表
     * @methodName: saveSysNotice
     * @param sysNoticeDTO:sysNoticeDTO数据传输对象
     * @return: ApiResult
     * @throws Exception
     * @date: 2020/06/06 23:01:20
     * @author: rabbit
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult saveSysNotice(SysNoticeDTO sysNoticeDTO) throws Exception {
        SysNotice  sysNotice = sysNoticeDTO.convert(SysNotice.class);
        boolean bool = super.save(sysNotice);
        if(bool){
            return ApiResult.ok(MsgEnum.ADD_SUCCESS.getDesc());
        }else{
            return ApiResult.fail(MsgEnum.ADD_FAIL.getDesc());
        }
    }
    /**
     * @Description 修改通知公告表
     * @methodName: modifySysNotice
     * @param  sysNoticeDTO:sysNoticeDTO数据传输对象
     * @return: ApiResult
     * @throws Exception
     * @date: 2020/06/06 23:01:20
     * @author: rabbit
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult modifySysNotice(SysNoticeDTO sysNoticeDTO) throws Exception {
        SysNotice  sysNotice = sysNoticeDTO.convert(SysNotice.class);
        boolean bool  =  super.updateById(sysNotice);
        if(bool){
            return ApiResult.ok(MsgEnum.UPDATE_SUCCESS.getDesc());
        }else {
            return ApiResult.fail(MsgEnum.UPDATE_FAIL.getDesc());
        }
    }
    /**
     * @Description 删除通知公告表
     * @methodName: removeSysNotice
     * @param ids:id集合
     * @return: ApiResult
     * @throws Exception
     * @date: 2020/06/06 23:01:20
     * @author: rabbit
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult removeSysNotice(Long[] ids) throws Exception {
       boolean bool =  super.removeByIds(Arrays.asList(ids));
        if(bool){
            return ApiResult.ok(MsgEnum.DELETE_SUCCESS.getDesc());
        }else {
            return ApiResult.fail(MsgEnum.DELETE_FAIL.getDesc());
        }
    }
    /**
     * @Description 根据Id获取通知公告表详情
     * @methodName: getSysNoticeInfo
     * @param id: id
     * @return:  ApiResult
     * @throws Exception
     * @date: 2020/06/06 23:01:20
     * @author: rabbit
     */
    @Override
    public ApiResult  getSysNoticeById(Long id) throws Exception {
        if(ObjectUtil.isNull(id)){
            return ApiResult.fail(MsgEnum.PARAM_NOT_NULL.getDesc());
        }
        SysNotice sysNotice = super.getById(id);
        if(ObjectUtil.isNull(sysNotice)){
            return ApiResult.fail(MsgEnum.OBJECT_NOT_EXIST.getDesc());
        }
        SysNoticeVO sysNoticeVO  = sysNotice.convert(SysNoticeVO.class);
        return ApiResult.ok(sysNoticeVO);
    }

    /**
     * @Description 通知公告表分页列表
     * @methodName: getSysNoticePageList
     * @param sysNoticePageParam
     * @return: ApiResult
     * @throws Exception
     * @date: 2020/06/06 23:01:20
     * @author: rabbit
     */
    @Override
    public ApiResult getSysNoticePageList(SysNoticePageParam sysNoticePageParam) throws Exception {
        Page<SysNotice> page = new PageInfo<>(sysNoticePageParam, OrderItem.desc("create_time"));
        QueryWrapper<SysNotice> queryWrapper= new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(sysNoticePageParam.getNoticeTitle()),SysNotice::getNoticeTitle,sysNoticePageParam.getNoticeTitle())
                .eq(ObjectUtil.isNotNull(sysNoticePageParam.getNoticeType()),SysNotice::getNoticeType,sysNoticePageParam.getNoticeType())
                .eq(ObjectUtil.isNotNull(sysNoticePageParam.getStatus()),SysNotice::getStatus,sysNoticePageParam.getStatus())
                .gt(ObjectUtil.isNotNull(sysNoticePageParam.getBeginTime()),SysNotice::getCreateTime,sysNoticePageParam.getBeginTime())
                .lt(ObjectUtil.isNotNull(sysNoticePageParam.getEndTime()),SysNotice::getCreateTime,sysNoticePageParam.getEndTime());
        IPage<SysNoticeVO>  iPage = sysNoticeMapper.selectPage(page,queryWrapper).convert(sysNotice -> sysNotice.convert(SysNoticeVO.class));
        Paging<SysNoticeVO> paging =  new Paging<SysNoticeVO>(iPage);
        return ApiResult.ok(paging);
    }

}
